<template>
  <el-dialog
    :before-close="handleClose"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :title="globalData.title"
    :visible.sync="isDialog.visible"
    top="5vh"
    width="750px"
  >
    <el-form
      ref="submitFormRef"
      :model="submitForm"
      :rules="submitFormRules"
      label-position="right"
      label-width="auto"
    >
      <el-form-item label="订单" prop="orderIds">
        <el-button size="mini" type="primary" @click="handleSelect">
          选择订单
        </el-button>
        <div class="orderSelect">
          <el-tag v-for="(item, index) in globalData.orderIds" :key="index">
            {{ item.order_sn }}
          </el-tag>
        </div>
      </el-form-item>

      <el-form-item label="结算金额" prop="order_settle_commission">
        <el-input
          v-model="submitForm.order_settle_commission"
          :min="0"
          clearable
          placeholder="请输入结算金额"
          type="number"
        ></el-input>
      </el-form-item>
    </el-form>

    <div slot="footer">
      <el-button @click="handleClose">取消</el-button>
      <el-button
        :loading="isLoading.submit"
        type="primary"
        @click="handleSubmit"
      >
        确 定
      </el-button>
    </div>

    <!-- 选择订单-->
    <OrderSelect v-if="isDialog.select" @close="closeDialog"></OrderSelect>
  </el-dialog>
</template>

<script>
import { reconciliation } from "@/api/reconciliation";
import OrderSelect from "./select.vue";

export default {
  name: "Edit",
  components: {
    OrderSelect,
  },
  props: {
    info: {
      type: Object,
    },
  },
  data() {
    return {
      // 表单提交
      submitForm: {
        orderIds: [],
        order_settle_commission: "",
      },
      // 表单验证
      submitFormRules: {
        orderIds: [{ required: true, message: "请选择订单", trigger: "blur" }],
        order_settle_commission: [
          { required: true, message: "请输入结算金额", trigger: "blur" },
        ],
      },
      // 数据
      globalData: {
        title: "",
        orderIds: [],
      },
      // 加载状态
      isLoading: {
        submit: false,
      },
      // 弹窗
      isDialog: {
        visible: true,
        select: false,
      },
    };
  },
  created() {
    this.globalData.title = "对账";
  },
  methods: {
    // 提交
    handleSubmit() {
      this.$refs.submitFormRef.validate((valid) => {
        if (!valid) {
          return false;
        }
        this.isLoading.submit = true;

        reconciliation(this.submitForm)
          .then(() => {
            this.isLoading.submit = false;
            this.$emit("close");
          })
          .catch(() => {
            this.isLoading.submit = false;
          });
      });
    },
    // 选择订单
    handleSelect() {
      this.isDialog.select = true;
    },
    //关闭
    closeDialog(val) {
      if (Object.keys(val).length > 0) {
        this.submitForm.orderIds = val.selected.map((item) => item.order_id);
        this.globalData.orderIds = val.selected;
      }
      console.log(val, 1234);
      this.isDialog.select = false;
    },
    // 关闭
    handleClose() {
      this.$emit("close", "cancel");
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .el-textarea textarea {
  font-family: "Microsoft YaHei", "微软雅黑", Arial, sans-serif;
}

::v-deep .tox-tinymce {
  min-height: 500px;
}

.orderSelect {
  .el-tag {
    margin-right: 10px;
  }
}

.image-box {
  width: 100%;
  height: 100%;
}
</style>
